/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gl2jni;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.ToggleButton;


public class GL2JNIActivity extends Activity {

  //GL2JNIView mView;
  MyGLView mView;
  SeekBar seekBar1, seekBar2, seekBar3;
  float x, y, z;
  int rotation;
  boolean flipV, flipH;

  @Override
  protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    //mView = new GL2JNIView(getApplication());
    setContentView(R.layout.activity_main);
    mView = findViewById(R.id.glview);
    seekBar1 = findViewById(R.id.seek1);
    seekBar2 = findViewById(R.id.seek2);
    seekBar3 = findViewById(R.id.seek3);
    seekBar1.setMax(1000);
    seekBar2.setMax(1000);
    seekBar3.setMax(1000);
    seekBar1.setProgress(500);
    seekBar2.setProgress(500);
    seekBar3.setProgress(600);
    mView.setEye(0, 0, 1);
    seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        x = (progress - 500) / 100f;
        Log.e("tag", "x=" + x);
        mView.setEye(x, y, z);
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {

      }

      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {

      }
    });
    seekBar2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        y = (progress - 500) / 100f;
        Log.e("tag", "y=" + y);
        mView.setEye(x, y, z);
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {

      }

      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {

      }
    });
    seekBar3.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        z = (progress - 500) / 100f;
        Log.e("tag", "z=" + z);
        mView.setEye(x, y, z);
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {

      }

      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {

      }
    });
    ToggleButton toggleButtonV = findViewById(R.id.toggleV);
    toggleButtonV.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        flipV = isChecked;
        mView.updateTexVertex(rotation, flipV, flipH);
      }
    });
    ToggleButton toggleButtonH = findViewById(R.id.toggleH);
    toggleButtonH.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        flipH = isChecked;
        mView.updateTexVertex(rotation, flipV, flipH);
      }
    });
  }

  @Override
  protected void onPause() {
    super.onPause();
    mView.onPause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mView.onResume();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mView.destroy();
  }

  public void rotate(View view) {
    mView.rotateCamera();
  }

  public void rotate0(View view) {
    rotation = 0;
    mView.updateTexVertex(rotation, flipV, flipH);
  }

  public void rotate90(View view) {
    rotation = 90;
    mView.updateTexVertex(rotation, flipV, flipH);
  }

  public void rotate180(View view) {
    rotation = 180;
    mView.updateTexVertex(rotation, flipV, flipH);
  }

  public void rotate270(View view) {
    rotation = 270;
    mView.updateTexVertex(rotation, flipV, flipH);
  }
}
